package arrival;

import calendar.VisualCalendar;
import common.ImagePanel;
import dbhandler.Booking;
import dbhandler.Room;
import evthandler.EventMessage;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class BookingDialog extends javax.swing.JDialog {

    private VisualCalendar calendar;
    private RoomDraw roomDraw;
    private Booking booking;

    /**
     * Creates new form BookingDialog
     * @param parent
     * @param modal
     */
    public BookingDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setVisible(true);
    }

    /**
     *
     * @return
     */
    public Booking getBooking() {
        return booking;
    }

    /**
     *
     */
    private void createBooking() {
        Room thisRoom = new Room(roomDraw.getSelectedRoom());
        booking = new Booking();
        booking.setInDate(checkinTextField.getText());
        booking.setOutDate(checkoutTextField.getText());
        booking.setRoom(thisRoom);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        requestPnl = new javax.swing.JPanel();
        oceanViewChk = new javax.swing.JCheckBox();
        executiveChk = new javax.swing.JCheckBox();
        accessibleChk = new javax.swing.JCheckBox();
        typePnl = new javax.swing.JPanel();
        singleChk = new javax.swing.JCheckBox();
        doubleChk = new javax.swing.JCheckBox();
        suiteChk = new javax.swing.JCheckBox();
        checkinTextField = new javax.swing.JTextField();
        checkinLabel = new javax.swing.JLabel();
        checkoutLabel = new javax.swing.JLabel();
        checkoutTextField = new javax.swing.JTextField();
        searchBtn = new javax.swing.JButton();
        submitBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        logoPnl = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Hotel Lazy BMS - Booking");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setMaximumSize(new java.awt.Dimension(700, 32767));
        jPanel1.setMinimumSize(new java.awt.Dimension(700, 100));

        requestPnl.setBackground(new java.awt.Color(255, 255, 255));
        requestPnl.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Special Requests", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 1, 13))); // NOI18N
        requestPnl.setForeground(new java.awt.Color(255, 255, 255));

        oceanViewChk.setBackground(new java.awt.Color(255, 255, 255));
        oceanViewChk.setText("Ocean View");
        oceanViewChk.setEnabled(false);

        executiveChk.setBackground(new java.awt.Color(255, 255, 255));
        executiveChk.setText("Executive");
        executiveChk.setEnabled(false);

        accessibleChk.setBackground(new java.awt.Color(255, 255, 255));
        accessibleChk.setText("Accessible");
        accessibleChk.setEnabled(false);

        org.jdesktop.layout.GroupLayout requestPnlLayout = new org.jdesktop.layout.GroupLayout(requestPnl);
        requestPnl.setLayout(requestPnlLayout);
        requestPnlLayout.setHorizontalGroup(
            requestPnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(requestPnlLayout.createSequentialGroup()
                .addContainerGap()
                .add(requestPnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(executiveChk)
                    .add(accessibleChk)
                    .add(oceanViewChk))
                .add(33, 33, 33))
        );
        requestPnlLayout.setVerticalGroup(
            requestPnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(requestPnlLayout.createSequentialGroup()
                .add(oceanViewChk)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(executiveChk)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 21, Short.MAX_VALUE)
                .add(accessibleChk)
                .add(16, 16, 16))
        );

        typePnl.setBackground(new java.awt.Color(255, 255, 255));
        typePnl.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Room Type", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans", 1, 13))); // NOI18N
        typePnl.setForeground(new java.awt.Color(255, 255, 255));

        singleChk.setBackground(new java.awt.Color(255, 255, 255));
        singleChk.setText("Single");
        singleChk.setEnabled(false);

        doubleChk.setBackground(new java.awt.Color(255, 255, 255));
        doubleChk.setText("Double");
        doubleChk.setEnabled(false);

        suiteChk.setBackground(new java.awt.Color(255, 255, 255));
        suiteChk.setText("Suite");
        suiteChk.setEnabled(false);

        org.jdesktop.layout.GroupLayout typePnlLayout = new org.jdesktop.layout.GroupLayout(typePnl);
        typePnl.setLayout(typePnlLayout);
        typePnlLayout.setHorizontalGroup(
            typePnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(typePnlLayout.createSequentialGroup()
                .addContainerGap()
                .add(typePnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(singleChk)
                    .add(doubleChk)
                    .add(suiteChk))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        typePnlLayout.setVerticalGroup(
            typePnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(typePnlLayout.createSequentialGroup()
                .add(singleChk)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(doubleChk)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(suiteChk)
                .addContainerGap())
        );

        checkinTextField.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        checkinTextField.setEnabled(false);
        checkinTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkinTextFieldMouseClicked(evt);
            }
        });

        checkinLabel.setText("Check-in Date");

        checkoutLabel.setText("Check-out Date");

        checkoutTextField.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        checkoutTextField.setEnabled(false);
        checkoutTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                checkoutTextFieldMouseClicked(evt);
            }
        });

        searchBtn.setText("Search");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        submitBtn.setText("Submit");
        submitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitBtnActionPerformed(evt);
            }
        });

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        logoPnl.setBackground(new java.awt.Color(211, 211, 211));
        logoPnl.setMinimumSize(new java.awt.Dimension(100, 50));

        org.jdesktop.layout.GroupLayout logoPnlLayout = new org.jdesktop.layout.GroupLayout(logoPnl);
        logoPnl.setLayout(logoPnlLayout);
        logoPnlLayout.setHorizontalGroup(
            logoPnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 100, Short.MAX_VALUE)
        );
        logoPnlLayout.setVerticalGroup(
            logoPnlLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 50, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(cancelBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(submitBtn))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(typePnl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(requestPnl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 155, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(checkoutTextField)
                            .add(checkinLabel)
                            .add(checkinTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .add(checkoutLabel)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, searchBtn))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 52, Short.MAX_VALUE)
                        .add(logoPnl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {cancelBtn, submitBtn}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(checkinLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkinTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkoutLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkoutTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(searchBtn))
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, requestPnl, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, typePnl, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(logoPnl, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 383, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(submitBtn)
                    .add(cancelBtn))
                .addContainerGap())
        );

        logoPnl.add(new ImagePanel("logo.png"));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 612, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 620, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void checkinTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkinTextFieldMouseClicked
        calendar = new VisualCalendar(null, true);
        calendar.setLocationRelativeTo(checkinTextField);
        calendar.setAlwaysOnTop(true);
        calendar.setVisible(true);
        checkinTextField.setText(calendar.getDate());
    }//GEN-LAST:event_checkinTextFieldMouseClicked

    private void checkoutTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_checkoutTextFieldMouseClicked
        calendar = new VisualCalendar(null, true);
        calendar.setLocationRelativeTo(checkoutTextField);
        calendar.setAlwaysOnTop(true);
        calendar.setVisible(true);
        checkoutTextField.setText(calendar.getDate());
    }//GEN-LAST:event_checkoutTextFieldMouseClicked

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed
        roomDraw = new RoomDraw(jScrollPane1, checkinTextField.getText(), checkoutTextField.getText());
        roomDraw.draw();
    }//GEN-LAST:event_searchBtnActionPerformed

    private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitBtnActionPerformed
        if (checkinTextField.getText().equals("") || checkoutTextField.getText().equals("") || roomDraw.getSelectedRoom() == null) {
            new EventMessage("Error", "You must choose a check in date, check out date and a room in order to submit!").showMessage();
        } else {
            createBooking();
            dispose();
        }
    }//GEN-LAST:event_submitBtnActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        dispose();
    }//GEN-LAST:event_cancelBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                BookingDialog dialog = new BookingDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox accessibleChk;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelBtn;
    private javax.swing.JLabel checkinLabel;
    private javax.swing.JTextField checkinTextField;
    private javax.swing.JLabel checkoutLabel;
    private javax.swing.JTextField checkoutTextField;
    private javax.swing.JCheckBox doubleChk;
    private javax.swing.JCheckBox executiveChk;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel logoPnl;
    private javax.swing.JCheckBox oceanViewChk;
    private javax.swing.JPanel requestPnl;
    private javax.swing.JButton searchBtn;
    private javax.swing.JCheckBox singleChk;
    private javax.swing.JButton submitBtn;
    private javax.swing.JCheckBox suiteChk;
    private javax.swing.JPanel typePnl;
    // End of variables declaration//GEN-END:variables
}
